#include "ForLoopGraph.h"
#include "base/Utility.h"
#include"InputPin.h"
#include"OutputPin.h"
#define SetAttr(AttrName) SetAttribute(AttrName, ele->Attribute(AttrName));
bool ForLoopGraph::OnInit()
{
    GraphBase* loopG = context.FindGraphById(loopBodyGraphId);
    if (loopG != nullptr)
    {
        loopBody = loopG;
    }
    else {
        printf("ForLoopGraph not find loopGraph:%d\r\n", nextGraphId);
    }
    GraphBase* completeG = context.FindGraphById(completeGraphId);
    if (completeG != nullptr)
    {
        complete = completeG;
    }
    else {
        printf("ForLoopGraph not find completeGraph:%d\r\n", completeGraphId);
    }
    return false;
}

void ForLoopGraph::ParseXmlElement(TiXmlElement* ele)
{
    SetAttr("id")
        SetAttr("name")
        SetAttr("pos")
        SetAttr("size")
        SetAttr("onLoopBody")
        SetAttr("onComplete")
}

bool ForLoopGraph::SetAttribute(const char* attrName, const char* attrVal)
{
    if (strcmp("id", attrName) == 0)
    {

        graphId.val = atoi(attrVal);
    }
    else if (strcmp("pos", attrName) == 0)
    {
        int posX, posY;
        sscanf_s(attrVal, "%d,%d", &posX, &posY);
        pos.x.val = posX;
        pos.y.val = posY;
    }
    else if (strcmp("size", attrName) == 0)
    {
        int x, y;
        sscanf_s(attrVal, "%d,%d", &x, &y);
        size.x.val = x;
        size.y.val = y;
    }
    else if (strcmp("onLoopBody", attrName) == 0)
    {
        nextGraphId = atoi(attrVal);
    }
    else if (strcmp("onComplete", attrName) == 0)
    {
        nextGraphId = atoi(attrVal);
    }
    return false;
}

void ForLoopGraph::OnDraw()
{
    if (ipp == nullptr)
    {
        ipp = NewObj<InputPin>();
    }
    if (loopBodyOpp == nullptr)
    {
        loopBodyOpp = NewObj<OutputPin>();
    }
    if (compeleteOpp == nullptr)
    {
        compeleteOpp = NewObj<OutputPin>();
    }
    //ForLoopPin* flp = dynamic_cast<ForLoopPin*>(pin);
    //if (flp == nullptr)
    //{
    //    printf("ForLoopGraph error :pin is null \r\n");
    //}
    //else {
    //    if (loopOpp == nullptr)
    //        loopOpp->p = &flp->loopBody;//&loopBody->pin;
    //    if (compeleteOpp == nullptr)
    //        compeleteOpp->p = &flp->compeleteBody;//&loopBody->pin;
    //}

    glPushMatrix();
    glTranslatef(baseOffset.x.val, baseOffset.y.val, zIndex.val);
    glLineWidth(1);
    DrawBorder();
    glColor3f(1, 1, 1);


    glColor3f(1, 1, 1);
    std::wstring wstr = Utility::ConvertToWstring(graphName.val);
    wchar_t* str = const_cast<wchar_t*>(wstr.data());
    TrueTypeFont::GetInst()->drawText(str, pos.x.val + size.x.val / 4, pos.y.val + 50, zIndex.val, size.x.val, 20);

    ipp->pos.x.val = pos.x.val + 20;
    ipp->pos.y.val = pos.y.val + 15;
    ipp->Draw();

    loopBodyOpp->pos.x.val = pos.x.val + size.x.val - 20;
    loopBodyOpp->pos.y.val = pos.y.val + 15;
    loopBodyOpp->Draw();

    compeleteOpp->pos.x = pos.x.val + size.x.val - 20;
    compeleteOpp->pos.y = pos.y.val + 65;
    compeleteOpp->Draw();

    glPopMatrix();
}

void ForLoopGraph::onMouseButtonDown(int button, int x, int y)
{
    __super::onMouseButtonDown(button, x, y);
}

void ForLoopGraph::onMouseMove(int x, int y)
{
    __super::onMouseMove(x, y);
}

void ForLoopGraph::onMouseButtonUp(int button, int x, int y)
{
    __super::onMouseButtonUp(button, x, y);
}

BlueprintScriptLibrary::Class* ForLoopGraph::GetClass()
{
    return nullptr;
}

Vec2vbi ForLoopGraph::GetOutputPinPos(int index)
{
    return Vec2vbi();
}

void ForLoopGraph::Execute()
{
    if (flp != nullptr)
    {
        for (int i = 0;i < flp->startIndex.val;i++)
        {
            SolvePin(flp->loopBody);
        }
        SolvePin(flp->compeleteBody);
    }

   /* for (int i = startIndex.val;i < endIndex.val;i++)
    {
        if (loopBody != nullptr)
            loopBody->Execute();
    }
    if (complete != nullptr)
        complete->Execute();*/
}

FunctionPin* ForLoopGraph::GetFunctionPin()
{
    return nullptr;
}

void ForLoopGraph::ConnectTo(GraphBase* graph, int index)
{
    if (index >= GetOuputPinNum().val || graph == nullptr)
    {
        return;
    }
    if (graph->GetInputPinNum().val > 0)
    {
        if (index == 0)
        {
            loopBody = nextGraph = graph;
        }
        if (index == 1)
        {
            complete = graph;
        }
    }
}
